<template>
  <div class="mod-user">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4">
        <el-card body-style="height">
          <el-tree
            :data="departmentData"
            :props="departmentProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            default-expand-all
            @node-click="handleNodeClick">
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <span>{{ data.name }}</span>
                <el-tag size="mini" type="info">共 {{ data.total }} 人</el-tag>
            </span>
          </el-tree>
        </el-card>
      </el-col>
      <el-col :span="20">
        <!--头部操作按钮-->
        <el-row :gutter="10" class="mb8">
          <el-form size="mini" :inline="true" :model="dataForm" @keyup.enter.native="getAdminList()">
            <el-form-item label="用户名">
              <el-input v-model="dataForm.username" placeholder="用户名" clearable @clear="usernameClear()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="getAdminList()">搜索</el-button>
              <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
              <el-button v-if="isAuth('sys:admin:delete')" type="danger" @click="deleteHandle()"
                         :disabled="dataListSelections.length <= 0">批量删除
              </el-button>
            </el-form-item>
          </el-form>
        </el-row>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="userId"
            header-align="center"
            align="center"
            width="80"
            label="ID">
          </el-table-column>
          <el-table-column
            prop="username"
            header-align="center"
            align="center"
            label="用户名">
          </el-table-column>
          <el-table-column
            prop="email"
            header-align="center"
            align="center"
            label="邮箱">
          </el-table-column>
          <el-table-column
            prop="mobile"
            header-align="center"
            align="center"
            label="手机号">
          </el-table-column>
          <el-table-column
            prop="status"
            header-align="center"
            align="center"
            label="状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
              <el-tag v-else size="small">正常</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            width="180"
            label="创建时间">
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作">
            <template slot-scope="scope">
              <el-button v-if="isAuth('sys:user:update')" type="text" size="small"
                         @click="addOrUpdateHandle(scope.row.userId)">修改
              </el-button>
              <el-button v-if="isAuth('sys:user:delete')" type="text" size="small"
                         @click="deleteHandle(scope.row.userId)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-show="total > 0"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!--<el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">-->
        <!--</el-col>-->
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './admin-add-or-update'

export default {
  data() {
    return {
      dataForm: {
        username: '',
        departmentId: ''
      },
      departmentProps: {
        children: 'children',
        label: 'name'
      },
      dataList: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      // 部门
      departmentData: [],
      dataListLoading: false,
      // 选择的数据
      dataListSelections: [],
      addOrUpdateVisible: false,
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.getAdminList()
      this.getDepartmentList()
    },
    // 获取管理员列表
    getAdminList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/admin/search'),
        method: 'post',
        data: this.$http.adornData({
          username: this.dataForm.username,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          departmentId: this.dataForm.departmentId
        })
      }).then(({data}) => {
        if (data && data.code === 200) {
          this.dataList = data.page.data
          this.total = data.page.total
        } else {
          this.dataList = []
          this.total = 0
        }
        this.dataListLoading = false
      })
    },
    // 获取部门数据列表
    getDepartmentList() {
      this.$http({
        url: this.$http.adornUrl('/sys/department/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.departmentData = data.departmentList
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.dataForm.departmentId = data.id
      this.getAdminList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNum = 1
      this.getAdminList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNum = val
      this.getAdminList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 获取要修改管理员的信息
    getAdminInfoById(id) {
      let adminInfo = {}
      if (id !== undefined) {
        this.dataList.map(item => {
          if (item.userId === id) {
            adminInfo = item
          }
        })
      }
      return adminInfo
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        // 为子组件addOrUpdate传递部门数据
        this.$refs.addOrUpdate.departmentDataTree = this.departmentData
        this.$refs.addOrUpdate.dataFromAdmin = this.getAdminInfoById(id)
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      let userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.userId
      })
      this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/admin/delete'),
          method: 'post',
          data: this.$http.adornData(userIds, false)
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getAdminList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
    // 清除搜索框
    usernameClear () {
      this.dataForm.username = ''
      this.dataForm.departmentId = ''
    }
  }
}
</script>
